package cn.wokoba.dao.mapper;



import cn.wokoba.dao.pojo.RaffleActivityAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

//抽奖活动账户表
@Mapper
public interface RaffleActivityAccountMapper extends BaseMapper<RaffleActivityAccount> {


    @Update("""
             update raffle_activity_account
                    set total_count_surplus = total_count_surplus - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and total_count_surplus > 0\
            """)
    int updateActivityAccountSubtractionQuota(RaffleActivityAccount build);

    @Update("""
            update raffle_activity_account
                    set month_count_surplus = month_count_surplus - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and month_count_surplus > 0""")
    void updateActivityAccountMonthSubtractionQuota(RaffleActivityAccount build);

    @Update("""
                 update raffle_activity_account
                    set month_count_surplus = #{monthCountSurplus} - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId}\
            """)
    void updateActivityAccountMonthSurplusImageQuota(RaffleActivityAccount build);

    @Update("""
             update raffle_activity_account
                    set day_count_surplus = day_count_surplus - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and day_count_surplus > 0\
            """)
    void updateActivityAccountDaySubtractionQuota(RaffleActivityAccount build);

    @Update("""
            update raffle_activity_account
                    set day_count_surplus = #{dayCountSurplus} - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId}""")
    void updateActivityAccountDaySurplusImageQuota(RaffleActivityAccount build);

    @Select("""
            select user_id, activity_id, total_count, total_count_surplus, day_count,
                    day_count_surplus, month_count, month_count_surplus
                    from raffle_activity_account
                    where user_id = #{userId} and activity_id = #{activityId}""")
    RaffleActivityAccount queryActivityAccountByUserId(RaffleActivityAccount raffleActivityAccount);





    @Update("""
                update raffle_activity_account
                    set
                        total_count = total_count + #{totalCount},
                        total_count_surplus = total_count_surplus + #{totalCountSurplus},
                        day_count_surplus = day_count_surplus + #{dayCountSurplus},
                        month_count_surplus = month_count_surplus + #{monthCountSurplus},
                        update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId}\
            """)
    void updateAccountQuota(RaffleActivityAccount raffleActivityAccount);

    @Select("""
             select  user_id, activity_id, total_count, total_count_surplus, day_count,
                    day_count_surplus, month_count, month_count_surplus
                    from raffle_activity_account
                    where user_id = #{userId} and activity_id = #{activityId}\
            """)
    RaffleActivityAccount queryAccountByUserId(RaffleActivityAccount raffleActivityAccount);
}
